package com.itheima.anli;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

            String action = req.getParameter("action");
            if(action==null){
//               resp.setStatus(404);
               resp.getWriter().write("页面走丢了");
            }
        //需要判断是什么业务
        if (action.equals("login")){
            login(req,resp);
        } else if (action.equals("find")) {
            find(req,resp);
        }else if (action.equals("add")){
            add(req,resp);
        }
    }
    //添加用户
    private void add(HttpServletRequest req,HttpServletResponse resp) throws IOException{
        //接收前端页面传递过来的参数信息
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        String gender = req.getParameter("gender");
        //使用BeanUtils将数据封装到User对象中
//        BeanUtils bus = new BeanUtils(username, pwd, gender);

        //将信息以zhangsan,123,man 这种方式写出到项目根目录下的user.txt文件下
        String path = getServletContext().getRealPath("/user.txt");
        BufferedWriter bw = new BufferedWriter(new FileWriter(path, true));
        bw.write(username + "," + pwd + "," + gender);
        bw.newLine();
        bw.close();

        //重定向跳转向首页
        resp.sendRedirect(req.getContextPath()+"/index.jsp");




    }

    //查看用户
    private void find(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
//        req.getContextPath().
//        new BufferedReader(new FileReader())
        //解决响应中文乱码问题
        resp.setContentType("text/html;charset=utf-8");
        //读取项目下user.txt中的学生信息
        InputStream is = getServletContext().getResourceAsStream("/user.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line =null;
        List<BeanUtils> list = new ArrayList<>();
        while ((line=br.readLine())!=null){
            //需要将每一行数据封装成一个BeanUser对象
            String[] strs = line.split(",");
            BeanUtils user = new BeanUtils(strs[0], strs[1], strs[2]);
            list.add(user);
        }
        br.close();
        req.setAttribute("list",list);
        req.getRequestDispatcher("/find.jsp").forward(req,resp);


    }


    //登陆页面
    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //获取用户传递过来的用户名和密码
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        //将这个用户名和密码到数据库进行匹配
        if (username.equals("zhangsan")&& pwd.equals("123")){
            //登陆成功
            User user = new User();
            user.setUsername(username);
            req.getSession().setAttribute("user",user);
            resp.sendRedirect(req.getContextPath()+"/index.jsp");
        }else {
            //登录失败
            req.setAttribute("msg","用户名或者密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
